/*************************************************************************

    File: content-dr.pl
    Copyright (C) 2007 

    Programmer: Luciana Benotti

    This file is part of Frolog, version 0.1 (October 2007).

    Frolog is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Frolog is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Frolog; if not, write to the Free Software Foundation, Inc., 
    59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*************************************************************************/

:- module(describe,[describe/2,describe/1,getContentsRole/2]). % describe(+Atom,-ListofLiterals)  
					     % describe(-ListofLiterals)	
					     % getContents(+Atom,-ListofLiterals)				 

:- use_module(library(lists)).
:- use_module('KBInterfaces/RacerInterface/racer',
	[individual_instance/4,
	 individuals_related/5,
	 forget_concept_assertion/4,
	 forget_role_assertion/5,
	 add_concept_assertion/4,
	 add_role_assertion/5,
	 worldKB/2, 
	 clone_abox/3, 
	 delete_abox/2,
	 individual_direct_types/4,   %(+Link,+ABox,+Individual,-Concepts)   
	 concept_children/3, 	%(+Link,+Concept,-Children)
	 concept_instances/4,   %(+Link,+ABox,+Concept,-Individuals)
	 concept_subsumes/3
	]).

% describe(-ListofLiterals)
% gives a general description of the scenario from the players point of view.
describe(LocationDescription):-
	racer: worldKB(Link,GameABox),
	racer: concept_instances(Link,GameABox,here,[Here]),
	describeLocation(Here,LocationDescription).

% describeLocation(+Atom,-ListofLiterals).
% Describes the location of the player (that is not a room) recursively to it's container until it's a room
describeLocation(Location,Description):-
	racer: worldKB(Link,GameABox),
	racer: not(individual_instance(Link,GameABox,Location,'room')),
	getContents(Location,ContentsLocation),
	racer: individual_fillers(Link,GameABox,Location,'haslocation',[Container]),
	describeLocation(Container,DescriptionContainer),
	append([hold(Location,ContentsLocation)],DescriptionContainer,Description).
	
% describeLocation(+Atom,-ListofLiterals).
% When the location is a room it is described as any object. 
describeLocation(Location,Description):-
	racer: worldKB(Link,GameABox),
	racer: individual_instance(Link,GameABox,Location,'room'),
	describe(Location,Description).

% describe(+Atom,-ListofLiterals)
% Returns all the properties, contents, exits and parts of the Thing. 
describe(Thing,Description):-
	racer: worldKB(Link,GameABox),
	getProperties(Thing,Properties),
	getContents(Thing,Contents),
	getExits(Thing,Exits),
	getParts(Thing,Parts),
	append([Properties],[hold(Thing,Contents),hasdetail(Thing,Parts),hasexit(Thing,Exits)],Description).
	
% The following pred eliminates the special concepts but keeps the type and the color for instance
% These will be eliminated later when looking at the overlap in the semantics generated by the 
% reference generation module.  
getProperties(Thing,Properties):-
	racer: worldKB(Link,GameABox),
	racer: individual_direct_types(Link,GameABox,Thing,Concepts),  
	filterConcepts(Concepts,DescriptionConcepts),
	concepttoAssertion(Thing,DescriptionConcepts,Properties).
	
filterConcepts([],[]).
filterConcepts([C|BaseConcepts],Concepts):-
			assertionToConcept(C,CDesc),
			racer:worldKB(Link,_),
			racer:concept_subsumes(Link,CDesc,'special'),
			filterConcepts(BaseConcepts,Concepts).
filterConcepts(['player'|BaseConcepts],Concepts):-
			filterConcepts(BaseConcepts,Concepts).
filterConcepts([C|BaseConcepts],Concepts):-
			assertionToConcept(C,CDesc),
			racer:worldKB(Link,_),
			racer:concept_subsumes(Link,'(or object room exit)',CDesc),
			filterConcepts(BaseConcepts,Concepts).
filterConcepts([C|BaseConcepts],[C|Concepts]):-
			filterConcepts(BaseConcepts,Concepts).
			
assertionToConcept(As,CDesc):- As=..[C,I1,I2],!, concat('(some ',C,FPart),concat(FPart,' *top*)',CDesc).
assertionToConcept(As,CDesc):- As=..[CDesc|_].
	
concepttoAssertion(Thing,[],[]).
concepttoAssertion(Thing,[Concept|Concepts],[Property|Properties]):-
	Property=..[Concept,Thing],
	concepttoAssertion(Thing,Concepts,Properties).
	
getContentsRole(Thing,hold(Thing,Contents)) :-
	getContents(Thing,Contents).

getContents(Thing,[]):-
	racer: worldKB(Link,GameABox),
	racer: not(individual_instance(Link,GameABox,Thing,'open')).	
	
getContents(Thing,Contents):-
	racer: worldKB(Link,GameABox),
	racer: individual_instance(Link,GameABox,Thing,'open'),
	racer: individual_fillers(Link,GameABox,Thing,'hold',Contents).
	
getExits(Thing,[]):-
	racer: worldKB(Link,GameABox),
	racer: not(individual_instance(Link,GameABox,Thing,'room')).	
	
getExits(Thing,Exits):-
	racer: worldKB(Link,GameABox),
	racer: individual_instance(Link,GameABox,Thing,'room'),
	racer: individual_fillers(Link,GameABox,Thing,'hasexit',Exits).	 
	
getParts(Thing,Parts):-
	racer: worldKB(Link,GameABox),
	racer: individual_fillers(Link,GameABox,Thing,'hasdetail',Parts).	 
	

/* 
look (when sitting in the couch) -> describe(here)
You are in a brown couch.
There is a apple and a frog in the couch.
The couch is in a drawing-room.
There is a brown table in the drawing-room.
The drawing-room has a exit leading south.

Racer(Time for Command) -> (concept-instances here): 0
Racer(Time for Command) -> (individual-fillers couch1 contains): 0
Racer(Time for Command) -> (individual-fillers couch1 has-exit): 0
Racer(Time for Command) -> (individual-instance-p couch1 room): 0
Racer(Time for Command) -> (individual-fillers couch1 has-location): 10
Racer(Time for Command) -> (individual-instance-p empfang visible): 0
Racer(Time for Command) -> (individual-fillers empfang contains): 0
Racer(Time for Command) -> (individual-fillers empfang has-exit): 0
Racer(Time for Command) -> (individual-instance-p empfang room): 0
---------------------
look south -> parsing error
---------------------
look (when in the room)
look at the room -> describe(empfang)
There is the table and the couch in the drawing-room.
The drawing-room has a exit leading south.

Racer(Time for Command) -> (concept-instances here): 60
Racer(Time for Command) -> (individual-fillers empfang contains): 10
Racer(Time for Command) -> (individual-fillers empfang has-exit): 0
Racer(Time for Command) -> (individual-instance-p empfang room): 0
----------------------
inventory -> describe(myself)

Racer(Time for Command) -> (individual-fillers myself contains): 0
----------------------
look at the couch -> describe(couch1)
Racer(Time for Command) -> (individual-instance-p couch1 room): 0
Racer(Time for Command) -> (individual-direct-types couch1): 20
Racer(Time for Command) -> (all-role-assertions-for-individual-in-domain couch1): 0
Racer(Time for Command) -> (concept-subsumes-p (or object room exit) couch): 10
Racer(Time for Command) -> (concept-subsumes-p (or object room exit) brown): 0
Racer(Time for Command) -> (individual-instance? couch1 open): 0
Racer(Time for Command) -> (individual-fillers couch1 contains): 0

*/
	

